/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pope.jloading.downloader;

import java.io.File;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import pope.jloading.downloader.Config;
import pope.jloading.downloader.XmlOper;

public class ConfigFactory {
    
    
    /**
     * 载入已经存在的状态保存文件。
     * @param configPath
     * @return
     * @throws java.lang.Exception
     */
    public static Config loadConfig(String configPath) throws Exception{
        return new Config(XmlOper.getDocument(configPath), configPath);
    }
    
    public static Config loadConfig(File configFile) throws Exception{
        return new Config(XmlOper.getDocument(configFile), configFile.getAbsolutePath());
    }
    
    /**
     * 创建一个新的状态保存文件
     * @param url
     * @param savePath
     * @param length
     * @param threads
     * @throws javax.xml.transform.TransformerConfigurationException
     * @throws javax.xml.transform.TransformerException
     * @throws javax.xml.parsers.ParserConfigurationException
     * <pre>
     * <file name="短文件名" save="保存路径" length="长度" threads="线程数">
     *     <urls>
     *             <url src=""/>
     *     </urls>
     *     <pieces>
     *             <piece start="" pos="" end=""/>
     *             <piece start="" pos="" end=""/>
     *     </pieces>
     * </file>
     * </pre>
     */
    public static Config createConfig(String id, 
            String name, 
            String url, 
            String savePath, 
            long length, 
            int threads) throws 
            TransformerConfigurationException, 
            TransformerException, 
            ParserConfigurationException {
        String configPath = savePath + "_config";   // 状态保存文件的路径
        Document doc = XmlOper.newDocument();
        Element file = doc.createElement("file");
        file.setAttribute("id", id);
        file.setAttribute("name", name);
        file.setAttribute("save", savePath);
        file.setAttribute("length", (String.valueOf(length)));
        file.setAttribute("threads", (String.valueOf(threads)));
        doc.appendChild(file);

        Element us = doc.createElement("urls");
        Element u = doc.createElement("url");
        u.setAttribute("src", url);
        us.appendChild(u);
        
        Element pieces = doc.createElement("pieces");
        file.appendChild(us);
        file.appendChild(pieces);
        XmlOper.saveDocument(doc, configPath);
        return new Config(doc, configPath);
    }
   /**
    * 
    * @param id
    * @param name
    * @param url
    * @param savePath
    * @param length
    * @param threads
    * @param sid
    * @return
    * @throws TransformerConfigurationException
    * @throws TransformerException
    * @throws ParserConfigurationException
    */
    public static Config createConfig(String id, 
            String name, 
            String url, 
            String savePath, 
            long length, 
            int threads,
            String sid,
            String action) throws 
            TransformerConfigurationException, 
            TransformerException, 
            ParserConfigurationException {
        String configPath = savePath + "_config";   // 状态保存文件的路径
        Document doc = XmlOper.newDocument();
        Element file = doc.createElement("file");
        file.setAttribute("id", id);
        file.setAttribute("name", name);
        file.setAttribute("save", savePath);
        file.setAttribute("length", (String.valueOf(length)));
        file.setAttribute("threads", (String.valueOf(threads)));
        file.setAttribute("sid", sid);
        file.setAttribute("action", action);
        doc.appendChild(file);

        Element us = doc.createElement("urls");
        Element u = doc.createElement("url");
        u.setAttribute("src", url);
        us.appendChild(u);
        
        Element pieces = doc.createElement("pieces");
        file.appendChild(us);
        file.appendChild(pieces);
        XmlOper.saveDocument(doc, configPath);
        return new Config(doc, configPath);
    }

    
    
}

